package com.sojson.controller.po;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.MenuParamVerify;
import com.sojson.entity.po.Menu;
import com.sojson.entity.vo.MenuVo;
import com.sojson.enums.EEnvironmentUserType;
import com.sojson.result.Result;
import com.sojson.service.MenuService;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 菜单权限表控制器
 * 
 * @author liu
 * @date 2021-04-13
 */
@Api(tags = "菜单权限表管理")
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 单条插入
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    @PostMapping(value = "/insert")
    @PreAuthorize("@ss.hasPermi('/menu/insert')")
    public Object insert(@RequestBody Menu menu) throws SQLException {
        // 校验参数
        Result<?> paramResult = MenuParamVerify.insert(menu);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        menu.insertInit();
        menuService.insert(menu);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Menu menu) throws SQLException {
        return Result.success(menuService.insertList(menu));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Menu menu) throws SQLException {
        return Result.success(menuService.deleteByDto(menu));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = menuService.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<Menu> findAll = menuService.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody Menu menu) throws SQLException {
        int countByDto = menuService.countByDto(menu);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByDto")
    public Object findByDto(Menu menu) throws SQLException {
        List<Menu> findByDto = menuService.findByDto(menu);
        return Result.success(findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @DeleteMapping("/deleteById")
    @PreAuthorize("@ss.hasPermi('/menu/deleteById')")
    public Object deleteById(Integer id) throws SQLException {
        if (StringUtil.isBlankObject(id)) {
            return Result.success();
        }

        menuService.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(menuService.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Menu menu) throws SQLException {
        menuService.updateById(menu);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Menu menu) throws SQLException {
        return Result.success(menuService.updateStaticByIdList(menu));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Menu menu) throws SQLException {
        return Result.success(menuService.updateChangeByIdList(menu));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/menu/updatePartialById')")
    public Object updatePartialById(@RequestBody Menu menu) throws SQLException {
        // 校验参数
        Result<?> paramResult = MenuParamVerify.updatePartialById(menu);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        menu.updateInit();
        menuService.updatePartialById(menu);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Menu menu) throws SQLException {
        return Result.success(menuService.updateStaticPartialByIdList(menu));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Menu menu) throws SQLException {
        return Result.success(menuService.updateChangePartialByIdList(menu));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Object findById(Integer id) throws SQLException {
        Menu findById = menuService.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Menu> findByIdList = menuService.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param menu
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Menu menu) throws SQLException {
        ArrayList<Integer> findIdByDto = menuService.findIdByDto(menu);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        menuService.clear();
        return Result.success();
    }

    /**
     * 获取菜单列表
     * @throws IOException 
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/menu/list')")
    public Object list(MenuVo menu) throws IOException {
        menu.setUserId(TokenUtil.getUserId());
        List<Menu> menus = menuService.selectMenuList(menu);
        return Result.success(menus);
    }

    /**
     * 获取菜单下拉树列表
     * @throws IOException 
     */
    @GetMapping("/treeselect")
    @PreAuthorize("@ss.hasPermi('/menu/treeselect')")
    public Object treeselect(MenuVo menu) throws IOException {
        menu.setUserId(TokenUtil.getUserId());
        List<Menu> menus = menuService.selectMenuList(menu);
        return Result.success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     * 
     * @param roleId    角色ID
     * @return
     * @throws IOException 
     */
    @GetMapping(value = "/roleMenuTreeselect")
    @PreAuthorize("@ss.hasPermi('/menu/roleMenuTreeselect')")
    public Object roleMenuTreeselect(Integer roleId) throws IOException {
        List<Menu> menus = menuService.selectMenuList(TokenUtil.getUserId());
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        hashMap.put("menus", menuService.buildMenuTreeSelect(menus));
        return Result.success(hashMap);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     * @throws IOException 
     */
    @GetMapping("/getRouters")
    @PreAuthorize("@ss.login()")
    public Object getRouters() throws IOException {
        MenuVo menu = new MenuVo();
        menu.setUserId(TokenUtil.getUserId());
        menu.setEnvironment(EEnvironmentUserType.WEB.getCode());
        List<Menu> menus = menuService.getMenuTreeByUserId(menu);
        return Result.success(menuService.buildMenus(menus));
    }

    /**
     * 获取菜单列表
     * @throws IOException 
     */
    @GetMapping("/listWeb")
    @PreAuthorize("@ss.hasPermi('/menu/list')")
    public Object listWeb(MenuVo menu) throws IOException {
        List<Menu> menus = menuService.list(menu);
        return Result.success(menus);
    }

}